package com.controller;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.Session;

import org.apache.ibatis.session.SqlSession;
import org.apache.tomcat.jni.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.LogDao;
import com.dao.Users;
import com.mysql.fabric.Response;
import com.util.MySqlSessionFactory;
@Controller
public class UsersController {
	@RequestMapping(value="/register.action",method=RequestMethod.POST)
	@ResponseBody
	public String register(
			@RequestParam(value="account",defaultValue="") String account,
			@RequestParam(value="password",defaultValue="") String password,
			@RequestParam(value="password2",defaultValue="") String password2
			){
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		LogDao dao1=sql.getMapper(LogDao.class);
		Map<String,Object> result=dao.query(account);
		if(!password.equals(password2)){
			return "ERROR_PASSWORD2";
		}
		if("1".equals(result.get("counter").toString())){
			return "ERR";
			}
		dao.add(account, password, "2012-08-02");
		Map<String,Object> a=dao.queryid();
		dao1.addLog(a.get("id").toString(),"0","注册");
		sql.commit();
		return "OK";
	}
	
		@RequestMapping(value="/moneyout.action",method=RequestMethod.POST)
		@ResponseBody
		public String moneyout(
				@CookieValue(value="result",defaultValue="")String resultId,
				HttpServletResponse response,
				@RequestParam(value="date_time",defaultValue="") String date_time,
				@RequestParam(value="kind",defaultValue="") String kind,
				@RequestParam(value="place",defaultValue="") String place,
				@RequestParam(value="money_out",defaultValue="") String money_out,
				@RequestParam(value="remark",defaultValue="") String remark
				){
			SqlSession sql=MySqlSessionFactory.getSqlSession();
			Users dao=sql.getMapper(Users.class);
			LogDao dao1=sql.getMapper(LogDao.class);
			dao.addout(resultId,date_time,kind,place,money_out,remark);
			Map<String,Object> a=dao.queryid();
			dao1.addLog(resultId,a.get("id").toString(),"支出");
			sql.commit();
			return "OK";
		}
		@RequestMapping(value="/moneyin.action",method=RequestMethod.POST)
		@ResponseBody
		public String moneyin(
				@CookieValue(value="result",defaultValue="")String resultId,
				HttpServletResponse response,
				@RequestParam(value="date_time",defaultValue="") String date_time,
				@RequestParam(value="kind",defaultValue="") String kind,
				@RequestParam(value="place",defaultValue="") String place,
				@RequestParam(value="money_in",defaultValue="") String money_in,
				@RequestParam(value="remark",defaultValue="") String remark
				){
			SqlSession sql=MySqlSessionFactory.getSqlSession();
			Users dao=sql.getMapper(Users.class);
			LogDao dao1=sql.getMapper(LogDao.class);
			dao.addin(resultId,date_time,kind,place,money_in,remark);
			Map<String,Object> a=dao.queryid();
			dao1.addLog(resultId,a.get("id").toString(),"收入");
			sql.commit();
			return "OK";
		}
	
	@RequestMapping(value="/login.action",method=RequestMethod.POST)
	@ResponseBody
	public String login(
			@RequestParam(value="account",defaultValue="") String account,
			@RequestParam(value="password",defaultValue="") String password,	
			HttpServletResponse response){
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		LogDao dao1=sql.getMapper(LogDao.class);
		Map<String,Object> result=dao.queryone(account,password);
		if("0".equals(result.get("counter").toString())){
			return "ERR";
			}
		response.addCookie(new Cookie("result",result.get("id").toString()));
		dao1.addLog(result.get("id").toString(),"0","登陆");
		sql.commit();
		return "OK";
	}
	

	
	@RequestMapping(value="/queryUsers.action",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> queryUsers(
			@CookieValue(value= "result",defaultValue="")String resultId,
			HttpServletResponse response,
			@RequestParam(value="id",defaultValue="") String id){
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		Map<String,Object> result=dao.queryUsers(resultId);
		return result;
		}
	@RequestMapping(value="/querydate.action",method=RequestMethod.GET)
	@ResponseBody
	public  Map<String,Object> querydate(
			@CookieValue(value= "result",defaultValue="")String resultId,
			HttpServletResponse response){
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		 Map<String,Object> result=dao.querydate(resultId);
		return result;
		
	}
	@RequestMapping(value="/queryday1.action",method=RequestMethod.GET)
	@ResponseBody
	public List<Map> queryday1(
			@CookieValue(value= "result",defaultValue="")String resultId,
			HttpServletResponse response){
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		List<Map> result=dao.queryday1(resultId);
		return result;
		
	}
	@RequestMapping(value="/queryday2.action",method=RequestMethod.GET)
	@ResponseBody
	public List<Map> queryday2(
			@CookieValue(value= "result",defaultValue="")String resultId,
			HttpServletResponse response){
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		List<Map> result=dao.queryday2(resultId);
		return result;
		
	}
	@RequestMapping(value="/querymonth.action",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> querymonth(
			@CookieValue(value="result",defaultValue="") String  resultId,
			HttpServletResponse response){
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		Map<String,Object> result=dao.querymonth(resultId);
		
		sql.commit();
		return result;
		
	}
	@RequestMapping(value="query_money_kindin.action",method=RequestMethod.GET)	
	@ResponseBody
	public List<Map> query_money_kindin(@CookieValue(value="result",defaultValue="")String resultId,
			HttpServletResponse response){
		
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		List<Map> map=dao.query_money_kindin(resultId);
		
		    return map;
	}
	@RequestMapping(value="query_money_kindout.action",method=RequestMethod.GET)	
	@ResponseBody
	public List<Map> query_money_kindout(@CookieValue(value="result",defaultValue="")String resultId,
			HttpServletResponse response){
		
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		List<Map> map=dao.query_money_kindout(resultId);
		
		    return map;
	}

		
	
	@RequestMapping(value="/queryresult.action",method=RequestMethod.POST)
	@ResponseBody
	public List<Map> queryresult(
			@CookieValue(value="result",defaultValue="") String resultId,
			@RequestParam(value="date_time",defaultValue="") String date_time,
			@RequestParam(value="kind",defaultValue="") String kind,
			@RequestParam(value="place",defaultValue="") String place,
			@RequestParam(value="money_in",defaultValue="") String money_in,
			@RequestParam(value="money_out",defaultValue="") String money_out,
			@RequestParam(value="remark",defaultValue="") String remark,
			HttpServletResponse response
			){
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		List<Map> map=dao.queryresult(resultId, date_time, kind, place,
				money_out, money_in, remark);
		sql.commit();
		return map;
		
	}
	@RequestMapping(value="/cleanCookie.action",method=RequestMethod.POST)
	@ResponseBody
	public String cleanCookie(
			@CookieValue(value="result",defaultValue="")String resultId,
			HttpServletResponse response){
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		LogDao dao1=sql.getMapper(LogDao.class);
		dao1.addLog(resultId,"0","退出");
		response.addCookie(new Cookie("result",null));
		
		sql.commit();
		return "OK";
	}
	@RequestMapping(value="/query1.action",method=RequestMethod.GET)
	@ResponseBody
	public List<Map> query1(
			@CookieValue(value="result",defaultValue="") String resultId,
			@RequestParam(value="id",defaultValue="") String id,
		
			HttpServletResponse response
			){
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		List<Map> map=dao.query1(id,resultId);
		System.out.println(resultId);
		sql.commit();
		return map;
	}
	@RequestMapping(value="/delete.action",method=RequestMethod.POST)
	@ResponseBody
	public String  delete(
			@CookieValue(value="result",defaultValue="") String resultId,
			HttpServletResponse response,
			@RequestParam(value="id",defaultValue="") String id
		
			
			){
		SqlSession sql=MySqlSessionFactory.getSqlSession();
		Users dao=sql.getMapper(Users.class);
		dao.delete(resultId, id);
		sql.commit();
		
		return "q";
		
	}
	




}
